package com.example.anliku.service.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.example.anliku.Utils.RedisUtil;
import com.example.anliku.entity.*;
import com.example.anliku.mapper.*;
import com.example.anliku.service.ElasticSearchService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.example.anliku.service.ExampleService;
import org.springframework.web.bind.annotation.RequestParam;

import java.lang.System;
import java.util.*;


@Service
@Slf4j
public class ExampleServiceImpl implements ExampleService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private ExampleMapper exampleMapper;

    @Resource
    private KnowledgeMapper knowledgeMapper;

    @Resource
    private ClazzMapper clazzMapper;

    @Resource
    private ExampleSeeMapper exampleSeeMapper;

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private ExampletypeMapper exampletypeMapper;

    @Resource
    private ExampleClazzMapper exampleClazzMapper;

    @Resource
    private ExampleKnowledgeMapper exampleKnowledgeMapper;

    @Resource
    private ElasticSearchService elasticSearchService;

    @Resource
    private ExampleCommentMapper exampleCommentMapper;

    @Resource
    private DownloadMapper downloadMapper;


    @Override
    public Message deleteByPrimaryKey(Integer exampleId) {
        redisUtil.allDel();
        exampleClazzMapper.deleteByExampleClazzExampleId(exampleId);
        exampleCommentMapper.deleteByExampleCommentExampleId(exampleId);
        exampleKnowledgeMapper.deleteByExampleKnowledgeExampleId(exampleId);
        exampleKnowledgeMapper.deleteByExampleKnowledgeExampleId(exampleId);
        exampleSeeMapper.deleteByExampleSeeExampleId(exampleId);
        boolean es = elasticSearchService.delete(exampleId);
        System.out.println(es);
        if (es) {
            return exampleMapper.deleteByPrimaryKey(exampleId) > 0 ? Message.success(null) : Message.fail(null);
        } else {
            return Message.fail(null);
        }
    }

    @Override
    public Message insert(Example record) {
        return exampleMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Example record) {
        redisUtil.allDel();
        record.setExampleStartTime(new Date());
        exampleMapper.insertSelective(record);
        ExampleForES exampleForES = new ExampleForES();
        exampleForES.setExampleId(record.getExampleId());
        exampleForES.setExampleName(record.getExampleName());
        exampleForES.setExampleStartTime(record.getExampleStartTime());
        exampleForES.setExampleState(record.getExampleState());
        exampleForES.setExampleTypeName(record.getExampleTypeName());
        exampleForES.setExampleKnowledgeId(JSON.toJSONString(""));
        exampleForES.setExampleClazz(JSON.toJSONString(""));
        exampleForES.setExampleImage(record.getExampleImage());
        exampleForES.setExampleIntroduction(record.getExampleIntroduction());
        exampleForES.setExampleUserName(record.getExampleUserName());
        exampleForES.setExamplePublicState(record.getExamplePublicState());
        exampleForES.setExampleDownload(0);
        Boolean es = elasticSearchService.insert(exampleForES);
        if (es) {
            redisUtil.set("download" + record.getExampleId(), 0);
            redisUtil.set("comment" + record.getExampleId(), 0);
            return Message.success(null).add(record.getExampleId());
        } else {
            return Message.fail(null);
        }
    }

    @Override
    public Message selectByPrimaryKey(Integer exampleId) {
        Example example = exampleMapper.selectByPrimaryKey(exampleId);
        HashMap map = new HashMap();
        map.put("example", example);
        List<ExampleKnowledge> knowledgeList = exampleKnowledgeMapper.selectAllByExampleKnowledgeExampleId(exampleId);
        List<ExampleClazz> clazzList = exampleClazzMapper.selectAllByExampleClazzExampleId(exampleId);
        List<Comment> commentList = commentMapper.selectAllByCommentCaseId(String.valueOf(exampleId));
        List<ExampleSee> userList = exampleSeeMapper.selectAllByExampleSeeExampleId(exampleId);
        Integer DownloadCase=downloadMapper.countByDownloadCaseId(exampleId.toString());
        Integer CommentCase=commentMapper.countByCommentCaseId(exampleId.toString());
        map.put("exampleDownloadNum", DownloadCase);
        map.put("CommentCaseNum", CommentCase);
        map.put("knowledgeList", knowledgeList);
        map.put("clazzList", clazzList);
        map.put("commentList", commentList);
        map.put("useList", userList);
        return Message.success(null).add(map);
    }

    @Override
    public HashMap Example(Integer exampleId) {
        Example example = exampleMapper.selectByPrimaryKey(exampleId);
        HashMap map = new HashMap();
        map.put("example", example);
        List<Knowledge> knowledgeList = new LinkedList<>();
        List<Clazz> clazzList = new LinkedList<>();
        List<Comment> commentList = new LinkedList<>();
        List<User> userList = new LinkedList<>();
        String[] ids = example.getExampleKnowledgeId().split(";");
        String[] ids1 = example.getExampleClazz().split(";");
        String[] ids2 = example.getExampleCommentId().split(";");
        String[] ids3 = example.getExampleSeeUserId().split(";");
        if (ids.length > 0) {
            for (String ID : ids) {
                Knowledge knowledge = knowledgeMapper.selectByPrimaryKey(Integer.parseInt(ID));
                knowledgeList.add(knowledge);
            }
        }
        if (ids1.length > 0) {
            for (String ID : ids1) {
                Clazz clazz = clazzMapper.selectByPrimaryKey(Integer.parseInt(ID));
                clazzList.add(clazz);
            }
        }
        if (ids2.length > 0) {
            for (String ID : ids2) {
                Comment comment = commentMapper.selectByPrimaryKey(Integer.parseInt(ID));
                commentList.add(comment);
            }
        }
        if (ids3.length > 0) {
            for (String ID : ids3) {
                User user = userMapper.selectByPrimaryKey(ID);
                userList.add(user);
            }
        }
        map.put("knowledgeList", knowledgeList);
        map.put("clazzList", clazzList);
        map.put("commentList", commentList);
        map.put("useList", userList);
        return map;
    }

    @Override
    public Message updateByPrimaryKeySelective(Example record) {

        redisUtil.allDel();

        if(record.getExampleId()==null){
            return Message.fail("ExampleId为空");
        }else if(record.getExampleName()==null){
            return Message.fail("ExampleName为空");
        }else if(record.getExampleTypeName()==null){
            return Message.fail("ExampleTypeName为空");
        }else if(record.getExampleImage()==null){
            return Message.fail("ExampleImage为空");
        }else if(record.getExampleIntroduction()==null){
            return Message.fail("ExampleIntroduction为空");
        }else if(record.getExampleUserName()==null){
            return Message.fail("ExampleUserName为空");
        }else if(record.getExampleBook()==null){
            return Message.fail("ExampleBook为空");
        }else if(record.getExampleVideo()==null){
            return Message.fail("ExampleVideo为空");
        }else if(record.getExampleCode()==null){
            return Message.fail("ExampleCode为空");
        }else if(record.getExamplePublicState()==null){
            return Message.fail("ExamplePublicState为空");
        }

        boolean es = elasticSearchService.updateAll(record.getExampleId());
        if (es) {
            return exampleMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
        } else {
            return Message.fail(null);
        }
    }

    @Override
    public Message updateByPrimaryKey(Example record) {
        return exampleMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectAll() {
        return Message.success(null).add(exampleMapper.selectAll());
    }

    @Override
    public Message selectKnowledgeAll() {
        boolean rs1 = redisUtil.hHasKey("selectExample", "selectKnowledgeAlls");
        if (rs1 == true) {
            return Message.success(null).add(redisUtil.hmget("selectExample").get("selectKnowledgeAlls"));
        }
        List<Map> mapList = new LinkedList<>();
        List<Example> exampleList = exampleMapper.selectAll();
        for (Example example : exampleList) {
            Integer exampleId = example.getExampleId();
            Example examplea = exampleMapper.selectByPrimaryKey(exampleId);
            HashMap map = new HashMap();
            map.put("example", examplea);
            List<ExampleKnowledge> knowledgeList = exampleKnowledgeMapper.selectAllByExampleKnowledgeExampleId(exampleId);
            List<ExampleClazz> clazzList = exampleClazzMapper.selectAllByExampleClazzExampleId(exampleId);
            List<ExampleComment> commentList = exampleCommentMapper.selectAllByExampleCommentExampleId(exampleId);
            List<ExampleSee> userList = exampleSeeMapper.selectAllByExampleSeeExampleId(exampleId);
            map.put("knowledgeList", knowledgeList);
            map.put("clazzList", clazzList);
            map.put("commentList", commentList);
            map.put("useList", userList);
            mapList.add(map);
        }

        Map returnMap = new HashMap();
        returnMap.put("selectKnowledgeAlls", mapList);
        redisUtil.hmset("selectExample", returnMap);
        return Message.success(null).add(mapList);
    }

    @Override
    public Message updateExampleStateAndExampleCheckIdAndExampleAdviceByExampleId(Integer updatedExampleState, String updatedExampleCheckId, String deductedExampleAdvice, Integer exampleId) {
        redisUtil.allDel();
        exampleMapper.updateExampleStateAndDecExamplePassTimeAndExampleCheckIdAndDecExampleAdviceByExampleId(updatedExampleState, new Date(), updatedExampleCheckId, deductedExampleAdvice, exampleId);
        if (updatedExampleState == 1) {
            boolean es = elasticSearchService.updateAll(exampleId);
            if (es) {
                return Message.success(null);
            } else {
                return Message.fail(null);
            }
        } else {
            return Message.success(null);
        }
    }


    @Override
    public Message searchTest(@RequestParam Integer pageNum, @RequestParam Integer size) {
        PageHelper.startPage(pageNum, size);
        List<Example> exampleList = exampleMapper.selectAll();
        PageInfo pageInfo = new PageInfo(exampleList);
        pageInfo.getTotal();
        return Message.success(null).add(pageInfo);
    }

    @Override
    public Message selectexampleUserIdByexample(String exampleUserId) {
        boolean rs1 = redisUtil.hHasKey("selectexampleUserIdByexample", "selectexampleUserIdByexample"+exampleUserId);
        if (rs1 == true) {
            return Message.success(null).add(redisUtil.hmget("selectexampleUserIdByexample").get("selectexampleUserIdByexample"+exampleUserId));
        }
        List<Example> ss = exampleMapper.selectexampleUserIdByexample(exampleUserId);
        Map returnMap = new HashMap();
        returnMap.put("selectexampleUserIdByexample", ss);
        redisUtil.hmset("selectexampleUserIdByexample"+exampleUserId, returnMap);

        return Message.success(null).add(ss);
    }

    @Override
    public Message selectAllByExampleUserIdAndExamplePublicState(String exampleUserId, Integer examplePublicState) {
        return Message.success(null).add(exampleMapper.selectAllByExampleUserIdAndExamplePublicState(exampleUserId, examplePublicState));
    }

    @Override
    public Message returnAll() {
        List<Exampletype> exampletypeList = exampletypeMapper.selectAll();
        List<Map> mapList = new LinkedList<>();
        for (Exampletype exampletype : exampletypeList) {
            HashMap hashMap = new HashMap();
            hashMap.put("name", exampletype.getExampleTypeCategoryName());
//            List<Example> exampleList = exampleMapper.selectAllByExampleTypeNameAndExamplePublicStateAndExampleState(exampletype.getExampleTypeCategoryName(),1,1)
//            for (Example example : exampleList){
//                HashMap hashMap1 = new HashMap();
//                List<ExampleKnowledge> knowledgeList = exampleKnowledgeMapper.selectAllByExampleKnowledgeExampleId(example.getExampleId());
//                List<ExampleClazz> clazzList = exampleClazzMapper.selectAllByExampleClazzExampleId(example.getExampleId());
//                hashMap1.put("example",example);
//                hashMap1.put("knowledgeList", knowledgeList);
//                hashMap1.put("clazzList", clazzList);
//            }
            hashMap.put("list", exampleMapper.selectAllByExampleTypeNameAndExamplePublicStateAndExampleState(exampletype.getExampleTypeCategoryName(), 1, 1));
            mapList.add(hashMap);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public Message selectByExampleUserId(String exampleUserId) {

        boolean rs1 = redisUtil.hHasKey("selectByExampleUserId", "selectByExampleUserId"+exampleUserId);
        System.out.println(rs1);
        if (rs1 == true) {
            return Message.success(null).add(redisUtil.hmget("selectByExampleUserId").get("selectByExampleUserId"+exampleUserId));
        }

        List<Map> mapList = new LinkedList<>();
        List<Example> exampleList = exampleMapper.selectByExampleUserId(exampleUserId);
        for (Example example : exampleList) {
            Integer exampleId = example.getExampleId();
            Example examplea = exampleMapper.selectByPrimaryKey(exampleId);
            Integer DownloadCase=downloadMapper.countByDownloadCaseId(exampleId.toString());
            Integer CommentCase=commentMapper.countByCommentCaseId(exampleId.toString());
            HashMap map = new HashMap();
            map.put("example", examplea);
            map.put("exampleDownloadNum", DownloadCase);
            map.put("CommentCaseNum", CommentCase);
            List<ExampleKnowledge> knowledgeList = exampleKnowledgeMapper.selectAllByExampleKnowledgeExampleId(exampleId);
            List<ExampleClazz> clazzList = exampleClazzMapper.selectAllByExampleClazzExampleId(exampleId);
            map.put("knowledgeList", knowledgeList);
            map.put("clazzList", clazzList);
            mapList.add(map);
        }
        Map returnMap = new HashMap();
        returnMap.put("selectByExampleUserId"+exampleUserId, mapList);
        redisUtil.hmset("selectByExampleUserId", returnMap);

        return Message.success(null).add(mapList);
    }

    @Override
    public Message ruturnTop5() {
        List<Map> mapList = new LinkedList<>();
        List<Example> exampleList = exampleMapper.selectAllByExamplePublicStateAndExampleState(1, 1);
        for (Example example : exampleList) {
            List<ExampleKnowledge> knowledgeList = exampleKnowledgeMapper.selectAllByExampleKnowledgeExampleId(example.getExampleId());
            List<ExampleClazz> clazzList = exampleClazzMapper.selectAllByExampleClazzExampleId(example.getExampleId());
            HashMap map = new HashMap();
            map.put("exampleId",example.getExampleId());
            map.put("exampleName",example.getExampleName());
            map.put("exampleImage",example.getExampleImage());
            map.put("exampleTypeName",example.getExampleName());
            map.put("exampleKnowledgeId",knowledgeList);
            map.put("exampleClazz",clazzList);
            map.put("exampleUserName",example.getExampleUserName());
            map.put("exampleComment", redisUtil.get("comment" + example.getExampleId()));
            map.put("exampleDownload",redisUtil.get("download" + example.getExampleId()));
            mapList.add(map);
        }
        HashMap maps = new HashMap();
        maps.put("list",mapList);
        return Message.success(null).add(maps);
    }

}



